package system

import (
	"encoding/json"
	"sensitives/service/dic"
	"strconv"
	"strings"

	"github.com/gin-gonic/gin"
)

type signup struct{}

var Signup signup

type setfield struct {
	Label     string
	Title     string
	Describe  string
	Value     string
	ListValue []string
}

type signupsens struct {
	Index    int           `json:"index"`
	IsArr    int           `json:"isArr"`
	Key      string        `json:"key"`
	NewIndex []interface{} `json:"newIndex"`
	Word     []string      `json:"word"`
	Level    uint8         `json:"level"`
	Type     string        `json:"type"`
}

var Signupsens []signupsens

// 只检测图集的内容字段
func (sg *signup) Check(gc *gin.Context) {
	siteid, _ := strconv.Atoi(gc.Param("siteid"))
	type Params struct {
		Title    string `form:"title"`
		Content  string `form:"content"`
		Setfield string `form:"setfield"`
	}
	var params Params
	err := gc.Bind(&params)
	Signupsens = make([]signupsens, 0)
	type OutPut struct {
		Code bool         `json:"code"`
		Data []signupsens `json:"data"`
	}
	output := OutPut{Code: false, Data: Signupsens}

	stive_sys, _ := json.Marshal(output)
	if err != nil {
		gc.Writer.Write(stive_sys)
		return
	}
	site_sys_dic := dic.CloudMap[siteid].Sys
	// 标题检测
	if params.Title != "" {
		sg.filter(params.Title, "title", &site_sys_dic)
	}
	// 内容检测
	if params.Content != "" {
		sg.filter(params.Content, "description", &site_sys_dic)
	}
	if params.Setfield != "" {
		var sfield []setfield
		err := json.Unmarshal([]byte(params.Setfield), &sfield)
		if err == nil {
			for k, field := range sfield {
				sg.setField(field, k, &site_sys_dic)
			}
		}
	}
	output = OutPut{Code: true, Data: Signupsens}
	stive_sys, _ = json.Marshal(output)
	gc.Writer.Write(stive_sys)
}

func (sg *signup) filter(text, types string, sys_dic *dic.TrieTree) {
	texts := strings.Split(text, "")
	texts_length := len(texts)
	for i := 0; i < texts_length; i++ {
		tmpText := texts[i]
		TempMap := sys_dic
		if _, ok := TempMap.Node[tmpText]; !ok {
			continue
		}
		TempMap = TempMap.Node[tmpText]
		if TempMap.IsEnd {
			sensi := signupsens{
				Index:    0,
				IsArr:    0,
				Key:      "",
				NewIndex: []interface{}{},
				Level:    TempMap.Level,
				Word:     []string{"", tmpText, ""},
				Type:     types,
			}
			Signupsens = append(Signupsens, sensi)
		}
		for j := i + 1; j < texts_length; j++ {
			jtext := texts[j]
			Tmpj, ok := TempMap.Node[jtext]
			if !ok {
				break
			} else if ok && Tmpj.IsEnd {
				tmpText += jtext
				sensi := signupsens{
					Index:    0,
					IsArr:    0,
					Key:      "",
					NewIndex: []interface{}{},
					Level:    Tmpj.Level,
					Word:     []string{"", tmpText, ""},
					Type:     types,
				}
				Signupsens = append(Signupsens, sensi)
				TempMap = Tmpj
			}
		}
	}
}

func (sg *signup) filterField(text, key string, index int, newIndex []interface{}, sys_dic *dic.TrieTree) {
	texts := strings.Split(text, "")
	texts_length := len(texts)
	for i := 0; i < texts_length; i++ {
		tmpText := texts[i]
		TempMap := sys_dic
		if _, ok := TempMap.Node[tmpText]; !ok {
			continue
		}
		TempMap = TempMap.Node[tmpText]
		if TempMap.IsEnd {
			sensi := signupsens{
				Index:    index,
				IsArr:    1,
				Key:      key,
				NewIndex: newIndex,
				Level:    TempMap.Level,
				Word:     []string{"", tmpText, ""},
				Type:     "setfield",
			}
			Signupsens = append(Signupsens, sensi)
		}
		for j := i + 1; j < texts_length; j++ {
			jtext := texts[j]
			Tmpj, ok := TempMap.Node[jtext]
			if !ok {
				break
			} else if ok && Tmpj.IsEnd {
				tmpText += jtext
				sensi := signupsens{
					Index:    index,
					IsArr:    1,
					Key:      key,
					NewIndex: newIndex,
					Level:    Tmpj.Level,
					Word:     []string{"", tmpText, ""},
					Type:     "setfield",
				}
				Signupsens = append(Signupsens, sensi)
				TempMap = Tmpj
			}
		}
	}
}

func (sg *signup) setField(sf setfield, index int, sys_dic *dic.TrieTree) {
	switch sf.Label {
	case "singleRowTem", "multiRowTem":
		sg.filterField(sf.Title, "title", index, []interface{}{index, "title"}, sys_dic)
		sg.filterField(sf.Describe, "describe", index, []interface{}{index, "describe"}, sys_dic)
		sg.filterField(sf.Value, "value", index, []interface{}{index, "value"}, sys_dic)
	case "radioGroupTem", "multipleChoiceTem", "spinnerTem":
		sg.filterField(sf.Title, "title", index, []interface{}{index, "title"}, sys_dic)
		sg.filterField(sf.Describe, "describe", index, []interface{}{index, "describe"}, sys_dic)
		if len(sf.ListValue) > 1 {
			for ik, text := range sf.ListValue {
				sg.filterField(text, "listvalue", index, []interface{}{index, "listvalue", ik}, sys_dic)
			}
		}
	case "activeDateTem", "accessoryTem":
		sg.filterField(sf.Title, "title", index, []interface{}{index, "title"}, sys_dic)
		sg.filterField(sf.Describe, "describe", index, []interface{}{index, "describe"}, sys_dic)
	}
}
